package com.zl.smartmall.library.im.vo;

import java.util.Collections;
import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;

import com.zl.smartmall.library.im.po.MessageInfo;

public class MessageInfoDao {

	private FinalDb mFinalDb;
	
	public MessageInfoDao(Context context) {
		mFinalDb = FinalDb.create(context);
	}
	
	public void insert(MessageInfo model) {
		synchronized (MessageInfoDao.class) {
			mFinalDb.save(model);
		}

	}
	
	public void delete() {
		synchronized (MessageInfoDao.class) {
			mFinalDb.deleteByWhere(MessageInfo.class, null);
		}
	}
	
	public void delete(int msgID) {
		synchronized (MessageInfoDao.class) {
			mFinalDb.deleteByWhere(MessageInfo.class, "msgID=" + msgID);
		}
	}

	/**
	 * 从表尾开始往上查询，结果已经进行了倒序
	 * @param rowIndex
	 * @return
	 */
	public List<MessageInfo> query(long relativeID, int rowIndex) {
		synchronized (MessageInfoDao.class) {
			List<MessageInfo> models = mFinalDb.findAllByWhere(
					MessageInfo.class, "relativeID=" + relativeID, "time desc limit " + rowIndex + ",10");
			Collections.reverse(models);
			return models;
		}
	}
	
}
